/*
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：系统配置服务                                                   
*│　作    者：sys                                              
*│　创建时间：2021-01-27 14:57:53   
*│　说    明：代码生成器生成   
*└──────────────────────────────────────────────────────────────┘
*/
using AutoMapper;
using SXFramWork.Core;
using SXFramWork.Model;
using SXFramWork.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SXFramWork.Service
{
    /// <summary>
	/// SysConfig服务层
	/// </summary>
    public class SysConfigService: ISysConfigService
    {
        private readonly ISysConfigRepository _repository;
        private readonly IMapper _mapper;

        public SysConfigService(ISysConfigRepository repository,IMapper mapper)
        {
            _mapper = mapper;
            _repository = repository;
        }

        public async Task<IApiResult> Add(ConfigAddModel model)
        {
            var entity = _mapper.Map<SysConfigEntity>(model);
            //更新或者新增
            var list =await _repository.GetAllAsync();
            if (list.Count() <= 0)
            {
                await _repository.InsertAsync(entity);
            }
            else
            {
                entity.Id = list.ToList()[0].Id;
                await _repository.UpdateAsync(entity);
            }
            return ApiResult.Ok();
        }

        public async Task<IApiResult> Query()
        {
            var list= await _repository.GetAllAsync();
            return ApiResult.Ok(list.ToList()[0]);
        }
    }
}